package cn.goour.utils.http;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpConfig implements Serializable {
	private static final long serialVersionUID = 2632058136291193491L;
	private Map<String, List<String>> backHeader;
	private int connectTimeout;
	private String data;
	private int readTimeout;
	private Map<String, String> sendHeader;
	private String url;

	public static HttpConfig getInstance() {
		return new HttpConfig();
	}

	public HttpConfig() {
		this.backHeader = new HashMap();
		this.sendHeader = new HashMap();
		this.connectTimeout = 30000;
		this.readTimeout = 30000;
		this.sendHeader.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		this.sendHeader.put("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
		this.sendHeader.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		this.sendHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0");
		this.sendHeader.put("Connection", "Keep-Alive");
	}

	public HttpConfig(String url2) {
		this();
		this.url = url2;
	}

	public HttpConfig(String url2, Map<String, String> head) {
		this();
		this.url = url2;
		this.sendHeader.putAll(head);
	}

	public HttpConfig(String url2, String data2) {
		this();
		this.url = url2;
		setData(data2);
	}

	public HttpConfig(String url2, String data2, Map<String, String> head) {
		this();
		this.url = url2;
		setData(data2);
		this.sendHeader.putAll(head);
	}

	public void setBackHeader(Map<String, List<String>> headerMap) {
		for (String key : headerMap.keySet()) {
			if (key != null) {
				Collection<? extends String> list = (List) headerMap.get(key);
				List<String> list2 = this.backHeader.get(key);
				if (list2 == null) {
					List<String> list3 = new ArrayList<>();
					list3.addAll(list);
					this.backHeader.put(key, list3);
				} else if (key.equals("Set-Cookie")) {
					list2.addAll(list);
					HashMap<String, Integer> map = new HashMap<>();
					int i = 0;
					while (i < list2.size()) {
						String cookieKey = list2.get(i).split("=")[0];
						if (map.containsKey(cookieKey)) {
							list2.set(map.get(cookieKey).intValue(), list2.get(i));
							list2.remove(i);
							i--;
						}
						map.put(cookieKey, Integer.valueOf(i));
						i++;
					}
				}
			}
		}
	}

	public Map<String, List<String>> getBackHeader() {
		return this.backHeader;
	}

	public String getBackHeaderCookieString() {
		List<String> ckie;
		StringBuffer re = new StringBuffer();
		if (!(this.backHeader == null || (ckie = this.backHeader.get("Set-Cookie")) == null)) {
			for (int i = 0; i < ckie.size(); i++) {
				re.append(String.valueOf(ckie.get(i).split(";")[0]) + ";");
			}
		}
		return re.toString();
	}

	public String getBackHeaderCookieMap() {
		StringBuffer re = new StringBuffer();
		List<String> ckie = this.backHeader.get("Set-Cookie");
		if (ckie != null) {
			for (int i = 0; i < ckie.size(); i++) {
				System.out.println(ckie);
				re.append(String.valueOf(ckie.get(i).split(";")[0]) + ";");
			}
		}
		return re.toString();
	}

	public String getUrl() {
		return this.url;
	}

	public void setUrl(String url2) {
		this.url = url2;
	}

	public String getData() {
		return this.data;
	}

	public void setData(String data2) {
		this.data = data2;
	}

	public String getReferer() {
		return this.sendHeader.get("Referer");
	}

	public void setReferer(String referer) {
		this.sendHeader.put("Referer", referer);
	}

	public String getUserAgent() {
		return this.sendHeader.get("User-Agent");
	}

	public void setUserAgent(String userAgent) {
		this.sendHeader.put("User-Agent", userAgent);
	}

	public void setHeader(String key, String value) {
		this.sendHeader.put(key.toLowerCase(), value);
	}

	public String getHeader(String key) {
		return this.sendHeader.get(key);
	}

	public Map<String, String> getSendHeader() {
		return this.sendHeader;
	}

	public int getConnectTimeout() {
		return this.connectTimeout;
	}

	public void setConnectTimeout(int connectTimeout2) {
		this.connectTimeout = connectTimeout2;
	}

	public int getReadTimeout() {
		return this.readTimeout;
	}

	public void setReadTimeout(int readTimeout2) {
		this.readTimeout = readTimeout2;
	}

	public void setAjax(boolean isAjax) {
		if (isAjax) {
			this.sendHeader.put("X-Requested-With", "XMLHttpRequest");
		} else {
			this.sendHeader.remove("X-Requested-With");
		}
	}

	public boolean isAjax() {
		String ajax = this.sendHeader.get("X-Requested-With");
		if (ajax == null || !ajax.equals("XMLHttpRequest")) {
			return false;
		}
		return true;
	}

	public void setDefaultAccept() {
		this.sendHeader.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
	}

	public void setJsonAccept() {
		this.sendHeader.put("Accept", "application/json, text/javascript, */*; q=0.01");
	}
}